<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (! Schema::hasTable('seller_groups')) {
            Schema::create('seller_groups', function (Blueprint $table) {
                $table->comment('商户组');
                $table->id();
                $table->decimal('fee_show_flat', 16, 4)->default(0.0)->comment('展示费固定值');
                $table->decimal('fee_show_percent', 16, 4)->default(0.0)->comment('展示费百分比');
                $table->decimal('fee_sale_flat', 16, 4)->default(0.0)->comment('销售费固定值');
                $table->decimal('fee_sale_percent', 16, 4)->default(0.0)->comment('销售费百分比');
                $table->integer('product_count')->default(0)->comment('商户可创建的商品数量，0不限制');
                $table->integer('position')->default(0)->comment('排序');
                $table->timestamps();
            });
        }
        if (! Schema::hasTable('seller_group_descriptions')) {
            Schema::create('seller_group_descriptions', function (Blueprint $table) {
                $table->comment('商户组名称、描述');
                $table->id()->comment('ID');
                $table->unsignedInteger('seller_group_id')->comment('商户组 ID')->index('seller_group_id');
                $table->string('locale', 10)->comment('语言');
                $table->string('name', 256)->comment('名称');
                $table->text('description')->comment('描述');
                $table->timestamps();
            });
        }
        if (! Schema::hasTable('sellers')) {
            Schema::create('sellers', function (Blueprint $table) {
                $table->comment('商户');
                $table->id();
                $table->unsignedInteger('seller_group_id')->comment('商户组 ID')->index('seller_group_id');
                $table->string('email')->comment('商户 Email');
                $table->string('telephone')->default('')->comment('电话号码');
                $table->string('store_name', 255)->default('')->comment('店铺名称');
                $table->string('logo', 255)->default('')->comment('logo');
                $table->string('banner', 255)->default('')->comment('横幅');
                $table->string('domain', 64)->nullable()->default(null)->comment('店铺域名');
                $table->timestamp('domain_update_at')->nullable()->comment('域名修改时间');
                $table->string('company', 256)->default('')->comment('公司名称');
                $table->text('description')->nullable()->comment('描述');
                $table->integer('position')->default(0)->comment('排序');
                $table->unsignedInteger('country_id')->default(0)->comment('国家 ID')->index('country_id');
                $table->unsignedInteger('zone_id')->default(0)->comment('省份 ID')->index('zone_id');
                $table->unsignedInteger('city_id')->default(0)->nullable()->comment('城市 ID')->index('city_id');
                $table->string('bank', 255)->default('')->comment('银行');
                $table->string('bank_account', 128)->default('')->comment('银行卡号');
                $table->string('bank_name', 255)->default('')->comment('银行卡户名');
                $table->integer('product_approve')->comment('商品是否需要审核');
                $table->integer('status')->comment('状态');
                $table->integer('approved')->default(true)->comment('审核状态，开启商户审核时代表是否已审核，status代表是否审核通过');
                $table->timestamps();
            });
        }
        if (! Schema::hasTable('seller_users')) {
            Schema::create('seller_users', function (Blueprint $table) {
                $table->comment('商户账号');
                $table->id()->comment('ID');
                $table->unsignedInteger('seller_id')->comment('商户 ID')->index('seller_id');
                $table->unsignedInteger('seller_user_group_id')->comment('商户账号组 ID')->index('seller_user_group_id');
                $table->string('email')->unique()->comment('Email');
                $table->string('password')->comment('密码');
                $table->string('name')->comment('用户名');
                $table->string('locale', 10)->comment('语言');
                $table->tinyInteger('is_root')->default(0)->comment('是否默认账号，即商户创建时创建的默认根账号');
                $table->tinyInteger('status')->default(0)->comment('状态');
                $table->softDeletes()->comment('删除时间');
                $table->timestamps();
            });
        }
        if (! Schema::hasTable('seller_user_groups')) {
            Schema::create('seller_user_groups', function (Blueprint $table) {
                $table->comment('商户账号组');
                $table->id()->comment('ID');
                $table->unsignedInteger('seller_id')->comment('商户 ID')->index('seller_id');
                $table->string('name', 256)->comment('名称');
                $table->text('description')->comment('描述');
                $table->timestamps();
            });
        }
        if (! Schema::hasTable('seller_transactions')) {
            Schema::create('seller_transactions', function (Blueprint $table) {
                $table->comment('商户交易流水');
                $table->id()->comment('ID');
                $table->unsignedInteger('seller_id')->nullable(false)->comment('商户 ID')->index('seller_id');
                $table->unsignedInteger('order_product_id')->default(null)->comment('订单商品 ID')->comment('对于商品销售收入费用的来源订单的order_product_id');
                $table->unsignedInteger('order_id')->default(null)->comment('订单 ID')->comment('对于商品销售收入费用的来源订单的order_id');
                $table->unsignedInteger('product_id')->default(null)->comment('商品 ID')->comment('对于商品上架费');
                $table->unsignedInteger('withdraw_id')->default(null)->comment('提现 ID')->comment('对于提现');
                $table->decimal('amount', 16, 4)->default(0.0)->comment('交易流水金额');
                $table->text('description')->comment('描述');
                $table->timestamps();
            });
        }
        if (! Schema::hasTable('seller_shippings')) {
            Schema::create('seller_shippings', function (Blueprint $table) {
                $table->comment('商户运费');
                $table->id()->comment('ID');
                $table->unsignedInteger('seller_id')->comment('商户 ID')->index('seller_id');
                $table->unsignedInteger('region_id')->comment('区域群组 ID')->index('region_id');
                $table->string('type', 32)->comment('运费类型(by_weight|by_flat)');
                $table->string('method', 32)->comment('运费计算方式(range|cumulative)');
                $table->float('flat', 32)->comment('固定附加费用（对于by_flat该值为固定运费值）');
                $table->json('rules')->comment('运费规则');
                $table->integer('priority')->comment('优先级（越小越优先）');
                $table->integer('status')->comment('状态');
                $table->timestamps();
            });
        }
        if (! Schema::hasTable('seller_withdraws')) {
            Schema::create('seller_withdraws', function (Blueprint $table) {
                $table->comment('商户提现');
                $table->id()->comment('ID');
                $table->unsignedInteger('seller_id')->comment('商户 ID')->index('seller_id');
                $table->timestamp('paid_at')->nullable()->default(null)->comment('支付时间');
                $table->string('bank', 128)->comment('银行名称');
                $table->string('bank_account', 128)->comment('银行卡号');
                $table->string('bank_name', 128)->comment('银行开户名');
                $table->string('currency_code', 32)->comment('货币');
                $table->decimal('amount', 16, 4)->default(0.0)->comment('提现金额');
                $table->string('description')->comment('备注');
                $table->integer('status')->comment('状态, 0为待处理，1为已处理');
                $table->timestamps();
            });
        }
        if (! Schema::hasColumn('products', 'seller_id')) {
            Schema::table('products', function (Blueprint $table) {
                $table->unsignedInteger('seller_id')->nullable()->comment('商户 ID')->index('seller_id');
                $table->integer('approved_status')->default(0)->comment('审核状态');
                $table->integer('position_seller')->default(0)->comment('排序');
                $table->timestamp('approved_at')->nullable()->default(null)->comment('审核时间');
                $table->string('approved_comment')->nullable()->default('')->comment('审核说明');
                $table->timestamp('date_available_from')->nullable()->default(null)->comment('上架日期');
                $table->timestamp('date_available_to')->nullable()->default(null)->comment('下架日期');
            });
        }
        if (! Schema::hasColumn('orders', 'seller_id')) {
            Schema::table('orders', function (Blueprint $table) {
                $table->unsignedInteger('seller_id')->comment('商户 ID')->default(0)->index('seller_id');
                $table->unsignedInteger('parent_id')->comment('订单 ID')->default(0)->index('parent_id');
            });
        }
        if (! Schema::hasColumn('order_products', 'seller_id')) {
            Schema::table('order_products', function (Blueprint $table) {
                $table->unsignedInteger('seller_id')->comment('商户 ID')->default(0)->index('seller_id');
                $table->decimal('store_commission_flat', 16, 4)->default(0.0)->comment('平台佣金固定值');
                $table->decimal('store_commission_percent', 16, 4)->default(0.0)->comment('平台佣金百分比');
                $table->decimal('seller_amount', 16, 4)->default(0.0)->comment('该订单商户得到的金额');
            });
        }
        // Schema::table('rmas', function (Blueprint $table) {
        //     $table->unsignedInteger('seller_id')->comment('商户 ID')->default(0)->index('seller_id');
        // });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        if (Schema::hasTable('seller_groups')) {
            Schema::dropIfExists('seller_groups');
        }
        if (Schema::hasTable('seller_group_descriptions')) {
            Schema::dropIfExists('seller_group_descriptions');
        }
        if (Schema::hasTable('sellers')) {
            Schema::dropIfExists('sellers');
        }
        if (Schema::hasTable('seller_users')) {
            Schema::dropIfExists('seller_users');
        }
        if (Schema::hasTable('seller_user_groups')) {
            Schema::dropIfExists('seller_user_groups');
        }
        if (Schema::hasTable('seller_transactions')) {
            Schema::dropIfExists('seller_transactions');
        }
        if (Schema::hasTable('seller_shippings')) {
            Schema::dropIfExists('seller_shippings');
        }
        if (Schema::hasColumn('products', 'approved_status')) {
            Schema::table('products', function (Blueprint $table) {
                $table->dropColumn('seller_id');
                $table->dropColumn('approved_at');
                $table->dropColumn('approved_comment');
                $table->dropColumn('approved_status');
                $table->dropColumn('position_seller');
                $table->dropColumn('date_available_from');
                $table->dropColumn('date_available_to');
            });
        }
        if (Schema::hasColumn('orders', 'seller_id')) {
            Schema::table('orders', function (Blueprint $table) {
                $table->dropColumn('seller_id');
                $table->dropColumn('parent_id');
            });
        }
        if (Schema::hasColumn('orders', 'seller_id')) {
            Schema::table('order_products', function (Blueprint $table) {
                $table->dropColumn('seller_id');
                $table->dropColumn('store_commission_flat');
                $table->dropColumn('store_commission_percent');
                $table->dropColumn('seller_amount');
            });
        }
        // Schema::table('rmas', function (Blueprint $table) {
        //     $table->dropColumn('seller_id');
        // });
    }
};
